package ModelLayer;
import java.util.*;

/**
 * Write a description of class SaleContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleContainer
{
    private ArrayList<Sale> saleContainer;
    private static SaleContainer instance;
    private Sale sObject;
    
    public SaleContainer()
    {
        saleContainer = new ArrayList<Sale>();
    }

    public static SaleContainer getInstance()
    {
        if(instance == null)
        {
            instance = new SaleContainer();
        }
        return instance;
    }

    public Sale findSaleById(int id)
    {
        sObject = null;
        int index = 0;
        boolean found = false;
        while(index < saleContainer.size() && !found)
        {

            if(sObject.getSaleId() == id)
            {
                sObject = saleContainer.get(index); 
                found = true;
            }
            else
            {
                index++;
            }
        }//end while
        return saleContainer.get(index);
    }

    public boolean addSale(Sale s)
    {
        boolean added = false;
        if(findSaleById(s.getSaleId()) ==null)
        {
            saleContainer.add(s);
            added = true;
        }
        return added;
    }

    public boolean deleteSaleById(int id)
    {
        if(saleContainer.contains(findSaleById(id)))
        {
            saleContainer.remove(findSaleById(id));
            return true;
        }
        else
        {
            return false;
        }
    }
}
